package com.xq.springboot.controller;

import java.util.List;
import java.util.Optional;

import javax.validation.Valid;

import com.xq.springboot.dao.jpa.GirlRepository;
import com.xq.springboot.domain.Girl;
import com.xq.springboot.domain.ResponseResult;
import com.xq.springboot.service.GirlService;
import com.xq.springboot.util.HttpResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Girl控制器类
 * 
 * @author 郭凯强
 */
@RestController
@RequestMapping("/girl")
public class GirlController {
	private static final Logger logger = LoggerFactory.getLogger(GirlController.class);
	@Autowired
	private GirlRepository girlRepository;
	// 测试事务使用的服务类
	@Autowired
	private GirlService girlService;

	/**
	 * 查询所有女生列表
	 * 
	 * @return
	 */
	@GetMapping(value = "/girls")
	public List<Girl> getGirlList() {
		return girlRepository.findAll();
	}

	/**
	 * 添加一个女生
	 * 
	 * @param cupSize
	 * @param age
	 * @return
	 */
	@PostMapping(value = "/girls")
	public ResponseResult addGirl(@Valid Girl girl, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			logger.error(bindingResult.getFieldError().getDefaultMessage());
			//~~~校验出错时, 返回null~~~
			return HttpResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
		return HttpResultUtil.success(girlRepository.save(girl));
	}

	@GetMapping(value = "/girls/{id}")
	public Optional<Girl> getGirl(@PathVariable(name = "id") Integer id) {
		return girlRepository.findById(id);
	}

	// 更新
	@PutMapping(value = "/girls/{id}")
	public Girl updateGirl(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize,
			@RequestParam("age") Integer age) {
		Girl girl = new Girl();
		girl.setId(id);
		girl.setCupSize(cupSize);
		girl.setAge(age);

		return girlRepository.save(girl);
	}

	// 删除
	@DeleteMapping(value = "/girls/{id}")
	public void delGirl(@PathVariable("id") Integer id) {
		girlRepository.deleteById(id);
	}

	// 通过年龄查询女生列表
	@GetMapping(value = "/girls/age/{age}")
	public List<Girl> getGirlListByAge(@PathVariable("age") Integer age) {
		return girlRepository.findByAge(age);
	}

	@PostMapping(value = "/girls/two")
	public void addTwoGirl() {
		girlService.insertTwo();
	}
	
	@GetMapping(value = "/girls/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws Exception{
        girlService.getAge(id);
    }
}
